package com.meihuayishu.backend.util;

import org.springframework.stereotype.Component;

import jakarta.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 增强版城市五行属性数据库
 * 支持动态调整城市五行权重
 */
@Component
public class EnhancedCityElementData {
    
    // 城市五行属性映射
    private final Map<String, String> cityElementMap = new HashMap<>();
    
    // 五行权重映射
    private final Map<String, Double> elementWeightMap = new HashMap<>();
    
    // 城市卦象权重调整映射（用于反馈闭环调整）
    private final Map<String, Map<String, Double>> cityHexagramAdjustMap = new ConcurrentHashMap<>();
    
    @PostConstruct
    public void init() {
        // 初始化城市五行属性
        // 北方城市 - 水
        cityElementMap.put("北京", "水");
        cityElementMap.put("天津", "水");
        cityElementMap.put("哈尔滨", "水");
        cityElementMap.put("长春", "水");
        cityElementMap.put("沈阳", "水");
        cityElementMap.put("呼和浩特", "水");
        
        // 南方城市 - 火
        cityElementMap.put("广州", "火");
        cityElementMap.put("深圳", "火");
        cityElementMap.put("海口", "火");
        cityElementMap.put("南宁", "火");
        cityElementMap.put("福州", "火");
        cityElementMap.put("厦门", "火");
        
        // 东方城市 - 木
        cityElementMap.put("上海", "木");
        cityElementMap.put("杭州", "木");
        cityElementMap.put("南京", "木");
        cityElementMap.put("合肥", "木");
        cityElementMap.put("济南", "木");
        cityElementMap.put("青岛", "木");
        
        // 西方城市 - 金
        cityElementMap.put("西安", "金");
        cityElementMap.put("兰州", "金");
        cityElementMap.put("西宁", "金");
        cityElementMap.put("银川", "金");
        cityElementMap.put("乌鲁木齐", "金");
        cityElementMap.put("拉萨", "金");
        
        // 中部城市 - 土
        cityElementMap.put("郑州", "土");
        cityElementMap.put("武汉", "土");
        cityElementMap.put("长沙", "土");
        cityElementMap.put("南昌", "土");
        cityElementMap.put("太原", "土");
        cityElementMap.put("石家庄", "土");
        
        // 初始化五行权重
        elementWeightMap.put("金", 1.2);
        elementWeightMap.put("木", 1.1);
        elementWeightMap.put("水", 1.3);
        elementWeightMap.put("火", 1.4);
        elementWeightMap.put("土", 1.0);
    }
    
    /**
     * 获取城市的五行属性
     * @param city 城市名称
     * @return 五行属性（金、木、水、火、土），如果城市不存在则返回"土"
     */
    public String getCityElement(String city) {
        return cityElementMap.getOrDefault(city, "土");
    }
    
    /**
     * 获取五行属性的权重
     * @param element 五行属性
     * @return 权重值，默认为1.0
     */
    public double getElementWeight(String element) {
        return elementWeightMap.getOrDefault(element, 1.0);
    }
    
    /**
     * 获取城市的五行权重
     * @param city 城市名称
     * @return 权重值
     */
    public double getCityElementWeight(String city) {
        String element = getCityElement(city);
        return getElementWeight(element);
    }
    
    /**
     * 获取城市对特定卦象的调整权重
     * @param city 城市名称
     * @param hexagramName 卦象名称
     * @return 调整权重，默认为1.0（不调整）
     */
    public double getCityHexagramAdjustment(String city, String hexagramName) {
        Map<String, Double> hexagramAdjustments = cityHexagramAdjustMap.get(city);
        if (hexagramAdjustments == null) {
            return 1.0;
        }
        return hexagramAdjustments.getOrDefault(hexagramName, 1.0);
    }
    
    /**
     * 根据用户反馈调整城市卦象权重
     * @param city 城市名称
     * @param hexagramName 卦象名称
     * @param isAccurate 是否准确
     */
    public void adjustCityHexagramWeight(String city, String hexagramName, boolean isAccurate) {
        cityHexagramAdjustMap.computeIfAbsent(city, k -> new ConcurrentHashMap<>());
        
        double currentWeight = getCityHexagramAdjustment(city, hexagramName);
        double adjustment = isAccurate ? 0.05 : -0.05; // 准确则增加权重，不准确则减少权重
        
        // 确保权重在合理范围内
        double newWeight = Math.max(0.5, Math.min(1.5, currentWeight + adjustment));
        cityHexagramAdjustMap.get(city).put(hexagramName, newWeight);
    }
    
    /**
     * 获取城市的综合权重（五行权重 * 卦象调整权重）
     * @param city 城市名称
     * @param hexagramName 卦象名称
     * @return 综合权重
     */
    public double getCombinedWeight(String city, String hexagramName) {
        double elementWeight = getCityElementWeight(city);
        double hexagramAdjustment = getCityHexagramAdjustment(city, hexagramName);
        return elementWeight * hexagramAdjustment;
    }
}